//
//  NIBaseDataManager.m
//  AIWAYS
//
//  Created by Marx on 2018/8/23.
//  Copyright © 2018年 wlq. All rights reserved.
//

#import "NIBaseDataManager.h"

@implementation NIBaseDataManager


- (BOOL)insertCoreDataWithModel:(NIBaseObject *)model AndName:(NSString *)name
{
    NSDictionary *dataDic = [model dictionaryRepresentation];
    
    NSInteger b =  [[CoreDataManager sharedCoreDataManager] insertCoreDataWithEntityName:name andDataDic:dataDic];
    
    if (b == CoreDataOperationSucceeded) {
        return YES;
    }else{
        return NO;
    }
}

- (BOOL)deleteCoreDataWithModel:(NIBaseObject *)model AndName:(NSString *)name
{
    NSDictionary *criterionDic = [model dictionaryRepresentation];
    NSInteger b = [[CoreDataManager sharedCoreDataManager] deleteCoreDataWithEntityName:name andCriterionDic:criterionDic];
    if (b == CoreDataOperationSucceeded) {
        return YES;
    }else{
        return NO;
    }
}

- (BOOL)updateCoreDataWithOriginalModel:(NIBaseObject *)model andPresentModel:(NIBaseObject *)presentModel AndName:(NSString *)name
{
    if ([self selectCoreDataWithModel:model AndName:name].count <= 0) {
       return [self insertCoreDataWithModel:presentModel AndName:name];
    }
    else
    {
        NSDictionary *criterionDic = [model dictionaryRepresentation];;
        NSDictionary *newDataDic = [presentModel dictionaryRepresentation];
        
        NSInteger b = [[CoreDataManager sharedCoreDataManager] updateCoreDataWithEntityName:name andCriterionDic:criterionDic andNewDataDic:newDataDic];
        if (b == CoreDataOperationSucceeded) {
            return YES;
        }else{
            return NO;
        }
    }
}

- (NSArray *)selectCoreDataWithModel:(NIBaseObject *)model AndName:(NSString *)name
{
//    NSMutableArray<(model.class) *> *messageModelArray = [[NSMutableArray<GWMessageModel *> alloc] init];
//    NSArray *objectArray =  [[CoreDataManager sharedCoreDataManager] selectCoreDataWithEntityName:@"Message_info" andCriterionDic:[messageModel dictionaryRepresentation]];
//    for (NSManagedObject *managedObject in objectArray) {
//        GWMessageModel *resultMessageModel = [[GWMessageModel alloc] init];
//        NSDictionary *attributeDictionary = [managedObject dictionaryWithValuesForKeys:[resultMessageModel.attributeMapDictionary allValues]];
//        [resultMessageModel setAttributesDictionary:attributeDictionary];
//        [messageModelArray addObject:resultMessageModel];
//    }
    Class class = [model class];
    NSMutableArray *modelArray = [[NSMutableArray alloc] init];
    NSArray *objectArray =  [[CoreDataManager sharedCoreDataManager] selectCoreDataWithEntityName:name andCriterionDic:[model dictionaryRepresentation]];
    for (NSManagedObject *managedObject in objectArray) {
        NIBaseObject * resultModel = [[class alloc] init];
        NSDictionary *attributeDictionary = [managedObject dictionaryWithValuesForKeys:[resultModel.attributeMapDictionary allValues]];
        [resultModel setAttributesDictionary:attributeDictionary];
        [modelArray addObject:resultModel];
    }
    return modelArray;
    
//    NSArray *objectArray =  [[CoreDataManager sharedCoreDataManager] selectCoreDataWithEntityName:name andCriterionDic:[model dictionaryRepresentation]];
//    return objectArray;
}
@end
